home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / vir_real / multi102.taz / multi102 / multiverse-1.0 / extensions / ext_bullet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-01  |  5.0 KB  |  167 lines

  1.  
  2. /**
  3.  
  4. #     #                                 #     #
  5. ##   ##  #    #  #        #####     #   #     #  ######  #####    ####   ######
  6. # # # #  #    #  #          #       #   #     #  #       #    #  #       #
  7. #  #  #  #    #  #          #       #   #     #  #####   #    #   ####   #####
  8. #     #  #    #  #          #       #    #   #   #       #####        #  #
  9. #     #  #    #  #          #       #     # #    #       #   #   #    #  #
  10. #     #   ####   ######     #       #      #     ######  #    #   ####   ######
  11.  
  12.  #####
  13. #     #   #   #   ####    #####  ######  #    #
  14. #          # #   #          #    #       ##  ##
  15.  #####      #     ####      #    #####   # ## #
  16.       #     #         #     #    #       #    #
  17. #     #     #    #    #     #    #       #    #
  18.  #####      #     ####      #    ######  #    #
  19.  
  20.     Copyright: Robert Grant, 1993
  21.  
  22. **/
  23.  
  24. #define _XOPEN_SOURCE
  25. #define _ALL_SOURCE
  26.  
  27. /* includes */
  28.  
  29. #include <stdio.h>
  30. #include <math.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33.  
  34. #include <graphics.h>
  35. #include <messages.h>
  36. #include <eventHandler.h>
  37. #include <clientManager.h>
  38. #include <objectManager.h>
  39.  
  40. #include <ext_bullet.h>
  41.  
  42. extern double matrix[4];
  43.  
  44. int initBullet(object_t *theBullet, bulletExt_t *bulletData,
  45.                                 init_t *event) {
  46.  
  47.     serverEventMsg_t ev;
  48.     object_t *parent;
  49.     createObject_t object;
  50.     changePanel_t panel;
  51.     mat rot;
  52.     parent = event->parent;
  53.     if (bulletData->fired)
  54.         return 0;
  55.     bulletData->fired = 2;
  56.     returnrotatematrix(&rot, 0, parent->coords.rot.y, 0);
  57.     matrix[0] = 0;
  58.     matrix[1] = 0;
  59.     matrix[2] = 0.7;
  60.     matrix[3] = 1;
  61.     transform(&rot);
  62.     theBullet->coords.loc.x = parent->coords.loc.x - matrix[0];
  63.     theBullet->coords.loc.z = parent->coords.loc.z + matrix[2];
  64.     theBullet->coords.rot.y = parent->coords.rot.y;
  65.     theBullet->coords.rot.x = 0;
  66.     theBullet->coords.loc.y = parent->coords.loc.y + 0.9;
  67.     strcpy(object.name, theBullet->name);
  68.     strcpy(object.type, theBullet->type);
  69.     memcpy(&object.coords, &theBullet->coords, sizeof(viewpoint));
  70.     object.class = MANAGED;
  71.     composeServerEventMessage(&ev, CREATE, OBJECT, NULL, &object);
  72.     broadcast(SERVEREVENT, sizeof(ev), &ev);
  73.     panel.type = PANEL_VALUE;
  74.     strcpy(panel.name, "dactyl:bullet");
  75.     panel.value = 0;
  76.     composeServerEventMessage(&ev, CHANGE, CHG_PANEL, "dactyl", &panel);
  77.     sendMessage(theBullet->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  78.     theBullet->collider = createCollider();
  79.     if (!theBullet->collider) {
  80.         fprintf(stderr, "Can't get space for collision detection!\n");
  81.         return 0;
  82.         }
  83.     theBullet->collider->obj = theBullet;
  84.     theBullet->collider->type = OBJ_DYNAMIC;
  85.     theBullet->collider->boundType = BOX;
  86.     theBullet->collider->box[0].x = -0.04;
  87.     theBullet->collider->box[0].y = -0.04;
  88.     theBullet->collider->box[0].z = -0.04;
  89.     theBullet->collider->box[1].x = 0.04;
  90.     theBullet->collider->box[1].y = 0.04;
  91.     theBullet->collider->box[1].z = 0.04;
  92.     addCollider(theBullet->collider);
  93.     return 1;
  94.     }
  95.  
  96. int moveBullet(object_t *theBullet, bulletExt_t *bulletData,
  97.                                         move_t *event) {
  98.  
  99.     serverEventMsg_t ev;
  100.     moveEvent_t move;
  101.     changePanel_t panel;
  102.     if (bulletData->fired == 2) {
  103.         if (theBullet->coords.loc.y < 0) {
  104.             bulletData->fired = 1;
  105.             }
  106.         else {
  107.             theBullet->coords.loc.x -= sin(RADIANS(theBullet->coords.rot.y))/8.0;
  108.             theBullet->coords.loc.z += cos(RADIANS(theBullet->coords.rot.y))/8.0;
  109.             theBullet->coords.loc.y -= theBullet->coords.rot.x;
  110.             theBullet->coords.rot.x += 0.001;
  111.             strcpy(move.name, theBullet->name);
  112.             memcpy(&move.coords, &theBullet->coords, sizeof(viewpoint));
  113.             composeServerEventMessage(&ev, MOVE, 0, NULL, &move);
  114.             broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  115.             return;
  116.             }
  117.         }
  118.     if (bulletData->fired == 1) {
  119.         ev.type = DELETE;
  120.         strcpy(ev.event.deleteEvent.name, theBullet->name);
  121.         broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  122.         panel.type = PANEL_VALUE;
  123.         strcpy(panel.name, "dactyl:bullet");
  124.         panel.value = 1;
  125.         composeServerEventMessage(&ev, CHANGE, CHG_PANEL, "dactyl", &panel);
  126.         sendMessage(theBullet->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  127.         removeCollider(theBullet->collider);
  128.         free(theBullet->collider);
  129.         theBullet->collider = NULL;
  130.         bulletData->fired = 0;
  131.         }
  132.     }
  133.  
  134. int checkBulletCollision(object_t *theBullet, bulletExt_t *bulletData,
  135.                             collideEvent_t *event) {
  136.  
  137.     serverEventMsg_t ev;
  138.     event_t term;
  139.     object_t *theCollider;
  140.     theCollider = event->obj;
  141.     if (theBullet->parent == theCollider)
  142.         return 0;
  143.     if (theBullet->parent == theCollider->parent)
  144.         return 0;
  145.     bulletData->fired = 1;
  146.     term.type = TERM_NOTIFY_EVENT;
  147.     sendEventByRef(theBullet->parent, &term);
  148.     return 1;
  149.     }
  150.  
  151. int addBulletExtensions(object_t *theObject) {
  152.  
  153.     bulletExt_t *temp = NULL;
  154.     if ((temp = (bulletExt_t *)malloc(sizeof(bulletExt_t))) == NULL) {
  155.         fprintf(stderr, "Can't allocate space for bullet extension!\n");
  156.         return 0;
  157.         }
  158.     addEventHandler(theObject, MOVE_EVENT, temp, moveBullet);
  159.     addEventHandler(theObject, INIT_EVENT, temp, initBullet);
  160.     addEventHandler(theObject, COLLISION_NOTIFY_EVENT, temp, checkBulletCollision);
  161.     }
  162.  
  163. int installBulletExtensions() {
  164.  
  165.     installEventHandlerCreator("bullet", addBulletExtensions);
  166.     }
  167.